package com.smartTerminal.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;


public class DateUtils {
	final static Logger logger = Logger.getLogger(DateUtils.class);

	private final static String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
	  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };

	public final static Date parseComplicateDate(String str) {
	 try{
	  if (StringUtils.isNullOrEmpty(str))
	   return null;
	  String[] strs = str.split(" ");
	  int nYear = Integer.parseInt(strs[5]);
	  int nMonth = 0;
	  for (int i = 0; i < months.length; i++) {
	   if (months[i].equals(strs[1])) {
	    nMonth = i;
	    break;
	   }
	  }
	  int nDay = Integer.parseInt(strs[2]);
	  String strTime = strs[3];
	  String[] strTimes = strTime.split(":");
	  int nHour = Integer.parseInt(strTimes[0]);
	  int nMinute = Integer.parseInt(strTimes[1]);
	  int nSecond = Integer.parseInt(strTimes[2]);
	  Calendar cal = new GregorianCalendar(nYear, nMonth, nDay, nHour,
	    nMinute, nSecond);
	  Date aDate = cal.getTime();
	  return aDate;
	 }
	 catch (ArrayIndexOutOfBoundsException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (NumberFormatException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (Exception e){
	  logger.warn("", e);
	  return null;
	 }
	}

	/**
	 * 返回用户指定日期的前或后n天的字符串 正数为向后n天,负数相反
	 * @param date
	 * @param day
	 * @return
	 * @throws ParseException 
	 */
	public static String  getNumToDate(String date,int day) throws ParseException
	{
	 
	 Calendar c = Calendar.getInstance();

	 SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
	 c.setTime(sdf1.parse(date));
	 c.add( Calendar.DAY_OF_MONTH, day );
	  
	 return sdf1.format(c.getTime());
	}

	public final static Date getYyyyMmDdHhMmss(String str) {
	 if (StringUtils.isNullOrEmpty(str))
	  return null;
	 try{
	  String[] strs = str.split(" ");
	  String[] strDays = strs[0].split("-");
	  int nYear = Integer.parseInt(strDays[0]);
	  int nMonth = Integer.parseInt(strDays[1]);
	  int nDay = Integer.parseInt(strDays[2]);

	  String[] strTimes = strs[1].split(":");
	  int nHour = Integer.parseInt(strTimes[0]);
	  int nMinute = Integer.parseInt(strTimes[1]);
	  int nSecond = Integer.parseInt(strTimes[2]);
	  Calendar cal = new GregorianCalendar(nYear, nMonth-1, nDay, nHour,
	    nMinute, nSecond);
	  return cal.getTime();
	 }
	 catch (ArrayIndexOutOfBoundsException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (NumberFormatException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (Exception e){
	  logger.warn("", e);
	  return null;

	 }
	}

	/**
	 * 返回与指定日期n个月的日期
	 * @param date
	 * @param day
	 * @return
	 * @throws ParseException 
	 */
	public static Date  getMonthToDate(Date date,int month) throws ParseException
	{
	 Calendar c = Calendar.getInstance();
	 c.setTime(date);
	       c.add(Calendar.DAY_OF_MONTH, -month*30);
	       return c.getTime();
	}

	/**
	 * 返回用户指定日期的前或后n天的字符串 正数为向后n天,负数相反
	 * @param date
	 * @param day
	 * @return
	 * @throws ParseException 
	 */
	public static Date  getNumToDate(Date date,int day) throws ParseException
	{
	 Calendar c = Calendar.getInstance();
	 c.add( Calendar.DAY_OF_MONTH, day );
	 return c.getTime();
	}

	/**
	 * 比较两个日期
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareDate(Date date1,Date date2)
	{
	 Calendar c = Calendar.getInstance();
	 c.setTime(date1);
	 c.add( Calendar.DAY_OF_MONTH, 1 );
	 return  c.getTime().getTime()>date2.getTime()?true:false;
	}


	public static void main(String[] args) throws ParseException {
	 SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
	 
	 System.out.println(autoCommit());
	 System.out.println(toHHmm(new Date()));
	 System.out.println(toYyyymmddHhmmss(new Date()));
	 System.out.println(toYyyyMmddHHmm(new Date()));
	 System.out.println(toHyphenatedYyyyMmDdHhMmSsFlexible(new Date()));
	 System.out.println(toHyphenatedYyyyMmDdHhMm(new Date()));
	 System.out.println(toHyphenatedYyyyMmdd(new Date()));
	 System.out.println(toYyyyMmdd(new Date()));
	 System.out.println(toHyphenatedYyMmdd(new Date()));
	 System.out.println(toYyMmdd(new Date()));
	 System.out.println(getYyMmDdNotHyphenated("180106"));
	 System.out.println(getYyMmDd("18-01-06"));
	 System.out.println(getYyyyMmDdNotHyphenated("20180106"));
	 System.out.println(getHhMm(new Date(),"15:59"));
	}

	/**
	 * 以如下格式转换日期
	 * @param str（2018-01-06 16:00:44）
	 * @return
	 */
	public final static Date getYyyyMmDdHhMm(String str) {
	 if (StringUtils.isNullOrEmpty(str))
	  return null;
	 try{
	  String[] strs = str.split(" ");
	  String[] strDays = strs[0].split("-");
	  int nYear = Integer.parseInt(strDays[0]);
	  int nMonth = Integer.parseInt(strDays[1]);
	  int nDay = Integer.parseInt(strDays[2]);

	  String[] strTimes = strs[1].split(":");
	  int nHour = Integer.parseInt(strTimes[0]);
	  int nMinute = Integer.parseInt(strTimes[1]);
	  int nSecond = 0;
	  Calendar cal = new GregorianCalendar(nYear, nMonth-1, nDay, nHour,
	    nMinute, nSecond);
	  return cal.getTime();
	 }
	 catch (ArrayIndexOutOfBoundsException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (NumberFormatException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (Exception e){
	  logger.warn("", e);
	  return null;

	 }
	}
	
	/**
	 * 以如下格式转换日期
	 * @param str（2018-01-06 16:00:44）
	 * @return
	 */
	public final static Date getYyyyMmDdHhMmSsFlexible(String str) {
	 if (StringUtils.isNullOrEmpty(str))
	  return null;
	 try{
	  String[] strs = str.split(" ");
	  String[] strDays = strs[0].split("-");
	  int nYear = Integer.parseInt(strDays[0]);
	  int nMonth = Integer.parseInt(strDays[1]);
	  int nDay = Integer.parseInt(strDays[2]);

	  int nHour = 0;
	  int nMinute = 0;
	  int nSecond = 0;
	  if (strs.length > 1){
	   String[] strTimes = strs[1].split(":");
	   if (strTimes.length > 1)
	    nHour = Integer.parseInt(strTimes[0]);
	   if (strTimes.length > 2)
	    nMinute = Integer.parseInt(strTimes[1]);
	   if (strTimes.length > 3)
	    nSecond = Integer.parseInt(strTimes[2]);
	  }
	  Calendar cal = new GregorianCalendar(nYear, nMonth-1, nDay, nHour,
	    nMinute, nSecond);
	  return cal.getTime();
	 }
	 catch (ArrayIndexOutOfBoundsException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (NumberFormatException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (Exception e){
	  logger.warn("", e);
	  return null;

	 }
	}
	/**
	 * 以某天为标准按以如下时间格式转换日期
	 * @param str（15:59）
	 * @return
	 */
	public final static Date getHhMm(Date date,String str) {
	 if (StringUtils.isNullOrEmpty(str))
	  return null;
	 try{
	  String[] strTimes = str.split(":");
	  int nHour = Integer.parseInt(strTimes[0]);
	  int nMinute = Integer.parseInt(strTimes[1]);
	  int nSecond = 0;
	  String strDay = toHyphenatedYyyyMmdd(date);
	  String[] strDays = strDay.split("-");
	  int nYear = Integer.parseInt(strDays[0]);
	  int nMonth = Integer.parseInt(strDays[1]);
	  int nDay = Integer.parseInt(strDays[2]);
	  Calendar cal = new GregorianCalendar(nYear, nMonth-1, nDay,nHour,
	    nMinute, nSecond);
	  return cal.getTime();
	 }
	 catch (ArrayIndexOutOfBoundsException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (NumberFormatException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (Exception e){
	  logger.warn("", e);
	  return null;

	 }
	}
	
	/**
	 * 以如下格式转换日期
	 * @param str（2018-01-06）
	 * @return
	 */
	public final static Date getYyyyMmDd(String str) {
	 if (StringUtils.isNullOrEmpty(str))
	  return null;
	 try{
	//   String[] strs = str.split(" ");
	  String[] strDays = str.split("-");
	  int nYear = Integer.parseInt(strDays[0]);
	  int nMonth = Integer.parseInt(strDays[1]);
	  int nDay = Integer.parseInt(strDays[2]);
	
	  Calendar cal = new GregorianCalendar(nYear, nMonth-1, nDay, 0,
	    0, 0);
	  return cal.getTime();
	 }
	 catch (ArrayIndexOutOfBoundsException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (NumberFormatException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (Exception e){
	  logger.warn("", e);
	  return null;
	 }
	}

	/**
	 * 以如下格式转换日期
	 * @param str（20180106）
	 * @return
	 */
	public final static Date getYyyyMmDdNotHyphenated(String str) {
	 if (StringUtils.isNullOrEmpty(str))
	  return null;
	 try{
	//   String[] strs = str.split(" ");
	  int nYear = Integer.parseInt(str.substring(0, 4));
	  int nMonth = Integer.parseInt(str.substring(4, 6));
	  int nDay = Integer.parseInt(str.substring(6, 8));

	  Calendar cal = new GregorianCalendar(nYear, nMonth-1, nDay, 0,
	    0, 0);
	  return cal.getTime();
	 }
	 catch (ArrayIndexOutOfBoundsException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (NumberFormatException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (Exception e){
	  logger.warn("", e);
	  return null;
	 }
	}

	/**
	 * 以如下格式转换日期；注意年份必须是2000后的年份
	 * @param str（18-01-06）
	 * @return
	 */
	public final static Date getYyMmDd(String str) {
	 if (StringUtils.isNullOrEmpty(str))
	  return null;
	 try{
	//   String[] strs = str.split(" ");
	  String[] strDays = str.split("-");
	  int nYear = Integer.parseInt(strDays[0]);
	  nYear += 2000;
	  int nMonth = Integer.parseInt(strDays[1]);
	  int nDay = Integer.parseInt(strDays[2]);

	  Calendar cal = new GregorianCalendar(nYear, nMonth-1, nDay, 0,
	    0, 0);
	  return cal.getTime();
	 }
	 catch (ArrayIndexOutOfBoundsException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (NumberFormatException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (Exception e){
	  logger.warn("", e);
	  return null;
	 }
	}

	/**
	 * 以如下格式转换日期；注意年份必须是2000后的年份
	 * @param str（180106）
	 * @return
	 */
	public final static Date getYyMmDdNotHyphenated(String str) {
	 if (StringUtils.isNullOrEmpty(str))
	  return null;
	 try{
	  int nYear = Integer.parseInt(str.substring(0,2));
	  nYear += 2000;
	  int nMonth = Integer.parseInt(str.substring(2,4));
	  int nDay = Integer.parseInt(str.substring(4,6));

	  Calendar cal = new GregorianCalendar(nYear, nMonth-1, nDay, 0,
	    0, 0);
	  return cal.getTime();
	 }
	 catch (ArrayIndexOutOfBoundsException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (NumberFormatException e){
	  logger.warn("", e);
	  return null;
	 }
	 catch (Exception e){
	  logger.warn("", e);
	  return null;
	 }
	}

	/**
	 * 返回时间的字符串格式如：180106
	 * @param aDate
	 * @return
	 */
	public final static String toYyMmdd(Date aDate){
	 if (aDate == null)
	  return "";
	 Calendar cal = new GregorianCalendar();
	 cal.setTime(aDate);
	 StringBuilder sb = new StringBuilder();
	 int nYear = cal.get(Calendar.YEAR);
	 nYear = nYear % 100;
	 int nMonth = cal.get(Calendar.MONTH);
	 nMonth++;
	 int nDay = cal.get(Calendar.DAY_OF_MONTH);
	 if (nYear < 10)
	  sb.append('0');
	 sb.append(nYear);
	 if (nMonth < 10)
	  sb.append('0');
	 sb.append(nMonth);
	 if (nDay < 10)
	  sb.append('0');
	 sb.append(nDay);
	 return sb.toString();
	}
	
	/**
	 * 返回时间的字符串格式如：18-01-06
	 * @param aDate
	 * @return
	 */
	public final static String toHyphenatedYyMmdd(Date aDate){
	 if (aDate == null)
	  return "";
	 Calendar cal = new GregorianCalendar();
	 cal.setTime(aDate);
	 StringBuilder sb = new StringBuilder();
	 int nYear = cal.get(Calendar.YEAR);
	 nYear = nYear % 100;
	 int nMonth = cal.get(Calendar.MONTH);
	 nMonth++;
	 int nDay = cal.get(Calendar.DAY_OF_MONTH);
	 if (nYear < 10)
	  sb.append('0');
	 sb.append(nYear);
	 sb.append('-');
	 if (nMonth < 10)
	  sb.append('0');
	 sb.append(nMonth);
	 sb.append('-');
	 if (nDay < 10)
	  sb.append('0');
	 sb.append(nDay);
	 return sb.toString();
	}
	
	
	/**
	 * 返回时间的字符串格式如：20180106
	 * @param aDate
	 * @return
	 */
	public final static String toYyyyMmdd(Date aDate){
	 if (aDate == null)
	  return "";
	 Calendar cal = new GregorianCalendar();
	 cal.setTime(aDate);
	 StringBuilder sb = new StringBuilder();
	 int nYear = cal.get(Calendar.YEAR);
	 int nMonth = cal.get(Calendar.MONTH);
	 nMonth++;
	 int nDay = cal.get(Calendar.DAY_OF_MONTH);

	 sb.append(nYear);
	 if (nMonth < 10)
	  sb.append('0');
	 sb.append(nMonth);
	 if (nDay < 10)
	  sb.append('0');
	 sb.append(nDay);
	 return sb.toString();
	}
	
	/**
	 * 返回时间的字符串格式如：2018-01-06
	 * @param aDate
	 * @return
	 */
	public final static String toHyphenatedYyyyMmdd(Date aDate){
	 if (aDate == null)
	  return "";
	 Calendar cal = new GregorianCalendar();
	 cal.setTime(aDate);
	 int nYear = cal.get(Calendar.YEAR);
	 int nMonth = cal.get(Calendar.MONTH);
	 nMonth++;
	 int nDay = cal.get(Calendar.DAY_OF_MONTH);

	 StringBuilder sb = new StringBuilder();
	 sb.append(nYear);
	 sb.append('-');
	 if (nMonth < 10)
	  sb.append('0');
	 sb.append(nMonth);
	 sb.append('-');
	 if (nDay < 10)
	  sb.append('0');
	 sb.append(nDay);
	 return sb.toString();
	}
	
	/**
	 * 返回时间的字符串格式如：2018-01-06 15:46
	 * @param aDate
	 * @return
	 */
	public final static String toHyphenatedYyyyMmDdHhMm(Date aDate){
	 if (aDate == null)
	  return "";
	 Calendar cal = new GregorianCalendar();
	 cal.setTime(aDate);
	 int nYear = cal.get(Calendar.YEAR);
	 int nMonth = cal.get(Calendar.MONTH);
	 nMonth++;
	 int nDay = cal.get(Calendar.DAY_OF_MONTH);
	 int nHour = cal.get(Calendar.HOUR_OF_DAY);
	 int nMinute = cal.get(Calendar.MINUTE);

	 StringBuilder sb = new StringBuilder();
	 sb.append(nYear);
	 sb.append('-');
	 if (nMonth < 10)
	  sb.append('0');
	 sb.append(nMonth);
	 sb.append('-');
	 if (nDay < 10)
	  sb.append('0');
	 sb.append(nDay);

	 sb.append(" ");
	 if (nHour < 10)
	  sb.append('0');
	 sb.append(nHour);
	 
	 sb.append(":");
	 if (nMinute < 10)
	  sb.append('0');
	 sb.append(nMinute);

	 return sb.toString();
	}
	
	/**
	 * 返回时间的字符串格式如：2018-01-06 15:45:49
	 * @param aDate
	 * @return
	 */
	public final static String toHyphenatedYyyyMmDdHhMmSsFlexible(Date aDate){
	 if (aDate == null)
	  return "";
	 Calendar cal = new GregorianCalendar();
	 cal.setTime(aDate);
	 int nYear = cal.get(Calendar.YEAR);
	 int nMonth = cal.get(Calendar.MONTH);
	 nMonth++;
	 int nDay = cal.get(Calendar.DAY_OF_MONTH);
	 int nHour = cal.get(Calendar.HOUR_OF_DAY);
	 int nMinute = cal.get(Calendar.MINUTE);
	 int nSecond = cal.get(Calendar.SECOND);

	 StringBuilder sb = new StringBuilder();
	 sb.append(nYear);
	 sb.append('-');
	 if (nMonth < 10)
	  sb.append('0');
	 sb.append(nMonth);
	 sb.append('-');
	 if (nDay < 10)
	  sb.append('0');
	 sb.append(nDay);

	 if (nHour > 0 ||
	   nMinute > 0 ||
	   nSecond > 0){
	  
	  sb.append(" ");
	  if (nHour < 10)
	   sb.append('0');
	  sb.append(nHour);

	  sb.append(":");
	  if (nMinute < 10)
	   sb.append('0');
	  sb.append(nMinute);

	  if (nSecond > 0){
	   sb.append(":");
		if (nSecond < 10)
	sb.append('0');
	   sb.append(nSecond);
	  }
	 }

	 return sb.toString();
	}

	/**
	 * 返回时间的字符串格式如：2018-01-06 15:46
	 * @param aDate
	 * @return
	 */
	public final static String toYyyyMmddHHmm(Date aDate){
	 if (aDate== null)
	  return "";
	 Calendar cal = new GregorianCalendar();
	 cal.setTime(aDate);

	 int nYear = cal.get(Calendar.YEAR);
	 int nMonth = cal.get(Calendar.MONTH);
	 nMonth++;
	 int nDay = cal.get(Calendar.DAY_OF_MONTH);
	 int nHour = cal.get(Calendar.HOUR_OF_DAY);
	 int nMinute = cal.get(Calendar.MINUTE);
	 
	 StringBuilder sb = new StringBuilder();
	 sb.append(nYear);
	 sb.append("-");
	 
	 if (nMonth < 10)
	  sb.append('0');
	 sb.append(nMonth);
	 sb.append("-");

	 if (nDay < 10)
	  sb.append('0');
	 sb.append(nDay);
	 sb.append(" ");

	 if (nHour < 10)
	  sb.append('0');
	 sb.append(nHour);
	 sb.append(":");
	 
	 if (nMinute < 10)
	  sb.append('0');
	 sb.append(nMinute);
	 return sb.toString();
	}

	/**
	 * 返回时间的字符串格式如：2018-01-06 15:45:49
	 * @param aDate
	 * @return
	 */
	public final static String toYyyymmddHhmmss(Date aDate){
	 if (aDate == null)
	  return "";
	 Calendar cal = new GregorianCalendar();
	 cal.setTime(aDate);
	 int nYear = cal.get(Calendar.YEAR);
	 int nMonth = cal.get(Calendar.MONTH);
	 nMonth++;
	 int nDay = cal.get(Calendar.DAY_OF_MONTH);
	 int nHour = cal.get(Calendar.HOUR_OF_DAY);
	 int nMInute = cal.get(Calendar.MINUTE);
	 int nSeconf= cal.get(Calendar.SECOND);

	 StringBuilder sb = new StringBuilder();
	 sb.append(nYear);
	 sb.append('-');
	 if (nMonth < 10)
	  sb.append('0');
	 sb.append(nMonth);
	 sb.append('-');
	 if (nDay < 10)
	  sb.append('0');
	 sb.append(nDay);

	 sb.append(' ');
	 
	 if (nHour < 10)
	  sb.append('0');
	 sb.append(nHour);
	 sb.append(':');
	 if (nMInute < 10)
	  sb.append('0');
	 sb.append(nMInute);
	 sb.append(':');
	 if (nSeconf < 10)
	  sb.append('0');
	 sb.append(nSeconf);
	 
	 return sb.toString();
	}

	/**
	 * 获取时间的几点几分的字符串格式
	 * @param aDate
	 * @return
	 */
	public final static String toHHmm(Date aDate){
	 if (aDate== null)
	  return "";
	 Calendar cal = new GregorianCalendar();
	 cal.setTime(aDate);

	 int nHour = cal.get(Calendar.HOUR_OF_DAY);
	 int nMinute = cal.get(Calendar.MINUTE);
	 
	 StringBuilder sb = new StringBuilder();
	 

	 if (nHour < 10)
	  sb.append('0');
	 sb.append(nHour);
	 sb.append(":");
	 
	 if (nMinute < 10)
	  sb.append('0');
	 sb.append(nMinute);
	 return sb.toString();
	}
	public static java.sql.Timestamp toTimestamp (Date theDate){
	 if (theDate== null)
	  return null;
	 return new Timestamp (theDate.getTime());
	}

	/**
	 * 判断当前的时小数是否在23:00---08:00内,如果在就自动commit到供应商下单
	 * @return
	 */
	public static boolean autoCommit(){
	   SimpleDateFormat sdf=new SimpleDateFormat("HH");
	      Date d=new Date();
	      String str=sdf.format(d);
	      int num=Integer.parseInt(str) ;
	       return (num>=23 || num<8)?true:false;
	}
	
	/**
	 * 获取当前日期的星期几
	 * @param date 传入时间
	 * @return 星期几
	 */
	public static String getWeekOfDate(Date date) {  
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(date);  
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;  
        if (w < 0)  
            w = 0;  
        return weekDays[w];  
    }  
}